package asionreachel.cn.smarthomedemo.base;

import java.util.ArrayList;

public class ASManager {

    private static ASManager s_Instance = new ASManager();

    public static ASManager GetInstance() {
        return s_Instance;
    }

    private ArrayList<BaseActivity> m_listActivity = new ArrayList<>();
    private ArrayList<BaseService> m_listService = new ArrayList<>();

    public ASManager() {
    }

    public void AddActivity( BaseActivity a_activity )
    {
        if( null == a_activity )
            return;

        for( int iIndex = 0 ; iIndex < m_listActivity.size() ; ++iIndex )
        {
            if( m_listActivity.get( iIndex ) == a_activity )
                return;
        }
        m_listActivity.add( a_activity );
    }

    public void DelActivity( BaseActivity a_activity )
    {
        for( int iIndex = 0 ; iIndex < m_listActivity.size() ; ++iIndex )
        {
            if( m_listActivity.get( iIndex ) == a_activity ) {
                m_listActivity.remove(iIndex);
                return;
            }
        }
    }

    public void FinishAllActivity()
    {
        ArrayList<BaseActivity> listActivity = new ArrayList<BaseActivity>( m_listActivity );
        while( listActivity.size() > 0 ) {
            if( null != listActivity.get( 0 ) )
                listActivity.get( 0 ).finish();
            listActivity.remove( 0 );
        }
    }

    public void AddService( BaseService a_service )
    {
        if( null == a_service )
            return;

        for( int iIndex = 0 ; iIndex < m_listService.size() ; ++iIndex )
        {
            if( m_listService.get( iIndex ) == a_service )
                return;
        }
        m_listService.add( a_service );
    }

    public void DelService( BaseService a_service )
    {
        for( int iIndex = 0 ; iIndex < m_listService.size() ; ++iIndex )
        {
            if( m_listService.get( iIndex ) == a_service ) {
                m_listService.remove(iIndex);
                return;
            }
        }
    }

    public void FinishAllService()
    {
        ArrayList<BaseService> listService = new ArrayList<BaseService>( m_listService );
        while( listService.size() > 0 ) {
            if( null != listService.get( 0 ) )
                listService.get( 0 );
            listService.remove( 0 );
        }
    }

    public void FinishAllAS( boolean a_bForceExit )
    {
        FinishAllActivity();
        FinishAllService();
        if( a_bForceExit )
            System.exit(0);
    }
}
